package model

import (
	"Walker/app/utils/data"
	"Walker/global"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"time"
)

type Model struct {
	Id        uint           `gorm:"id" json:"id"`
	UpdatedAt time.Time      `gorm:"updated_at" json:"updated_at"`
	CreatedAt time.Time      `gorm:"created_at" json:"created_at"`
	DeletedAt gorm.DeletedAt `gorm:"deleted_at" json:"deleted_at" swaggertype:"string"`
}

func UseCon() *gorm.DB {
	var Db *gorm.DB = global.DB
	return Db
}
func Paginate(ctx *gin.Context) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		strPage := ctx.Query("page")
		strPageSize := ctx.Query("page_size")
		var page int
		var pageSize int
		if page = data.StringToInt(strPage); page == 0 {
			page = 1
		}
		if pageSize = data.StringToInt(strPageSize); pageSize == 0 {
			pageSize = 10
		}
		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
